// \u4e00-\u9fa5] 中文验证规则
import validate from 'wechat-validate'
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    // 获取当前时间
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    // 已通过 审核的 房屋
    houseList: [],
    nothingList: [{
      name: '暂无可报修房屋',
      disabled: true
    }],
    // 维修项目列表
    repairItem: [],
    attachment: [],
    // 房屋信息
    house: '',
    // 房屋Id
    houseId: '',
    // 手机号
    mobile: '',
    // 维修项目
    repair: '',
    // 维修项目Id
    repairItemId: '',
    // 日期
    date: '',
    // 问题描述
    description: '',
    id: '',
  },
  // 校验规则
  rules: {
    // 房屋信息
    house: [{
      required: true,
      message: '请选择需要报修的房屋!',
    }],
    // 手机号
    repair: [{
      required: true,
      message: '请填写维修项目!',
    }],
    // 维修项目
    mobile: [{
        required: true,
        message: '当前手机号不能为空!',
      },
      {
        pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
        message: '请检查手机号码是否正确!',
      }
    ],
    // 日期
    date: [{
      required: true,
      message: '请选择上门维修日期',
    }],
    // 问题描述
    description: [{
      required: true,
      message: '当前问题描述不能为空!',
    }, {
      pattern: /^[\s\S]{0,200}$/,
      message: '问题描述在200个字符以内!',
    }],
    attachment: [{
      required: true,
      message: '当前附件信息不能为空!',
    }]
  },

  // 删除照片
  delImage(e) {
    // 通过 索引 筛选
    const attachment = this.data.attachment.filter((item, index) => index !== e.detail.index)
    //  更新附件
    this.setData({
      attachment
    })
  },

  openHouseLayer() {
    this.setData({
      houseLayerVisible: true,
    })
  },

  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false,
    })
  },

  openRepairLayer() {
    this.setData({
      repairLayerVisible: true,
    })
  },

  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true,
    })
  },

  /* 关闭弹窗 */
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false,
    })
  },

  async goList() {
    // 验证码 rules 中定义的全部数据
    const isAllValid = this.validate()
    if (!isAllValid) return

    const {
      id,
      houseId,
      mobile,
      repairItemId,
      date,
      description,
      attachment
    } = this.data

    id && await this.updateWarrantyInfo(id);
    !id && await this.submitWarrantyInfo()
    await wx.http.post('/repair', {
      houseId,
      mobile,
      appointment: date,
      repairItemId,
      description,
      attachment
    })

    wx.utils.toast('提交成功')

    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })    
  },

  /* 选择报修房屋 */
  getOptions(e) {
    this.setData({
      houseId: e.detail.id,
      house: e.detail.name
    })
  },

  /* 选择维修项目 */
  getRepair(e) {
    this.setData({
      repair: e.detail.name,
      repairItemId: e.detail.id
    })
  },

  /* 设置当前时间 */
  selectDateInfo(e) {
    const date = new Date(e.detail)
    this.setData({
      dateLayerVisible: false,
      date: getApp().formatDate(date),
    })
  },

  async onShow() {
    /* 获取审核通过的房屋信息 */
    const {
      data
    } = await wx.http.get('/house')
    /* 获取维修项目 */
    const repairRes = await wx.http.get('/repairItem')

    this.setData({
      houseList: data,
      repairItem: repairRes.data
    })
  },

  // 上传照片
  afterRead(e) {
    var _this = this
    const {
      file
    } = e.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://live-api.itheima.net/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().getToken()}`
      },
      success({
        data
      }) {
        const id = JSON.parse(data).data.id
        const url = JSON.parse(data).data.url
        // 上传完成需要更新 attachment
        const {
          attachment
        } = _this.data;
        attachment.push({
          ...{
            id,
            url,
          }
        });
        _this.setData({
          attachment
        });
      },
    });
  },

  async onLoad({
    id
  }) {
    // 更新页面头部标题
    id && wx.setNavigationBarTitle({
      title: '编辑报修信息',
    });
    // 获取房屋详情
    id && await this.getRepairInfo(id)

  },

  // 获取房屋详情
  async getRepairInfo(id) {
    const {
      data
    } = await wx.http.get(`/repair/${id}`)

    // 表单回显
    this.setData({
      id,
      date: data.appointment,
      attachment: data.attachment,
      description: data.description,
      house: data.houseInfo,
      houseId: data.id,
      mobile: data.mobile,
      repair: data.repairItemName,
      repairItemId: data.repairItemId
    })
  },

  // 提交报修房屋信息
  async submitWarrantyInfo() {
    const {
      houseId,
      mobile,
      repairItemId,
      date,
      description,
      attachment
    } = this.data
    await wx.http.post('/repair', {
      houseId,
      mobile,
      appointment: date,
      repairItemId,
      description,
      attachment
    })

  },

  // 更新报修房屋信息
  async updateWarrantyInfo(id) {
    const {
      houseId,
      mobile,
      repairItemId,
      date,
      description,
      attachment
    } = this.data
    await wx.http.post('/repair', {
      id,
      houseId,
      mobile,
      appointment: date,
      repairItemId,
      description,
      attachment
    })

  }
})